package view;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import entity.User;
import main.Main;
import util.Constants;

public class LoginJDialog extends JDialog {
	private static final long serialVersionUID = 1383140238205069483L;

	public LoginJDialog(){
		setTitle("系统登录");
		setResizable(false);
		setSize(320,180);
	    setLocationRelativeTo(null);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		
		setLayout(new BorderLayout());
        JPanel centerPane = new JPanel();
        add(centerPane, BorderLayout.CENTER);
        
		GridBagLayout layout = new GridBagLayout();
		centerPane.setLayout(layout);
		GridBagConstraints gbc = new GridBagConstraints();

		JLabel label11 = new JLabel();
		label11.setText("用户名");
		label11.setFont(Constants.font);
		
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.weightx=0.15;
		gbc.weighty=0.3333;
		gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.EAST;
        gbc.insets = new Insets(15,15,0,0);
        
		centerPane.add(label11, gbc);
	        
        JTextField userName = new JTextField();
        userName.enableInputMethods(false);
        
        gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.gridwidth = 2;
		gbc.gridheight = 1;
		gbc.weightx=0.85;
		gbc.weighty=0.3333;
		gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.insets = new Insets(15,0,0,15);
		centerPane.add(userName, gbc);

		JLabel label21 = new JLabel();
		label21.setText("密码");
		label21.setFont(Constants.font);
		 
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.weightx=0.15;
		gbc.weighty=0.3333;
		gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.EAST;
        gbc.insets = new Insets(0,15,0,0);
		centerPane.add(label21, gbc);
				
		JPasswordField password = new JPasswordField();
		password.setSize(240, 36);
		
		gbc.gridx = 1;
		gbc.gridy = 1;
		gbc.gridwidth = 2;
		gbc.gridheight = 1;
		gbc.weightx=0.85;
		gbc.weighty=0.3333;
		gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.insets = new Insets(0,0,0,15);
		centerPane.add(password, gbc);

		JButton btn = new JButton("登录");
		btn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				User user = new User();
                boolean result = user.login(userName.getText(),new String(password.getPassword()));
                if(!result){
                	JOptionPane.showMessageDialog(null,"用户名或密码错误!", "登录失败", JOptionPane.WARNING_MESSAGE);
                	return;
                }
                Main.user = user;
                dispose();
                new MainJFrame();
			}
		});

		
		
		
		////////////////////////
//		JButton btn1 = new JButton("admin");
//		btn1.addActionListener(new ActionListener(){
//			@Override
//			public void actionPerformed(ActionEvent e) {
//				User user = new User();
//                boolean result = user.login("admin","123456");
//                if(!result){
//                	JOptionPane.showMessageDialog(null,"用户名或密码错误!", "登录失败", JOptionPane.WARNING_MESSAGE);
//                	return;
//                }
//                Main.user = user;
//                setVisible(false);
//                new MainJFrame();
//			}
//		});
//		
//		JButton btn2 = new JButton("yw");
//		btn2.addActionListener(new ActionListener(){
//			@Override
//			public void actionPerformed(ActionEvent e) {
//				User user = new User();
//                boolean result = user.login("yw","123456");
//                if(!result){
//                	JOptionPane.showMessageDialog(null,"用户名或密码错误!", "登录失败", JOptionPane.WARNING_MESSAGE);
//                	return;
//                }
//                Main.user = user;
//                setVisible(false);
//                new MainJFrame();
//			}
//		});
//		
		JPanel p = new JPanel();
		p.add(btn);
//		p.add(btn1);
//		p.add(btn2);
		///////////////////
		
		
		
		gbc.gridx = 1;
		gbc.gridy = 2;
		gbc.gridwidth = 2;
		gbc.gridheight = 1;
		gbc.weightx=0.85;
		gbc.weighty=0.3333;
		gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.insets = new Insets(0,0,15,15);
//		centerPane.add(btn, gbc);
		centerPane.add(p, gbc);
				
		setVisible(true);
	}
	
}
